package com.yuanqi.dz.dc.service.impl;

import java.util.HashMap;
import java.util.Map;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;
import com.powermanager.consult.action.dz.trade.RefundInfoArgs;
import com.yuanqi.powermop2.core.ws.ExecuteCompWS;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;
import com.yuanqitech.powerrefund.channel.cmbc.MinshengSchuleGetResult;


public class MsHisServiceImpl implements DZDataInterface{
	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");

	@SuppressWarnings("unchecked")
	public DZInfoArray getDZData(String merchantnum, String dz_date) {
		DZInfoArray dzInfoArray = new DZInfoArray();
		//SQLCommandService sql  = PnbClientUtil.getSQLCommand();
		//List<Map<String, Object>> list = sql.getListMap("select * from  dz_main t where t.merchant_num ='"+merchantnum+"' and upper(mis_his) = upper('his')");
		//Map<String, Object> main = new HashMap<String, Object>();
		//if(null != list && list.size() > 0)
		//main = list.get(0);
		String returnmsg = null;
		try {
			returnmsg = new MinshengSchuleGetResult().returnCheckDZ(merchantnum, dz_date);
		} catch (Exception e) {
			LOG.addError("检查退费数据是否回写HIS异常"+e);
			dzInfoArray = new DZInfoArray();
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("检查退费数据是否回写HIS异常");
			return dzInfoArray;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if(null != returnmsg){
			LOG.addTrace("调用检查退费数据是否回写接口返回报文"+returnmsg);
			map= (Map<String, Object>) pnbclient.util.JSONParser.toObject(returnmsg, Map.class);
			String rc = String.valueOf(map.get("rc"));
			String msg = String.valueOf(map.get("msg"));
			String address = "TaskGetTrackBankCardCompareInfoWSXML_JAVA";
			if("1".equals(rc)){
				dz_date = dz_date.replaceAll("/", "");
				dz_date = dz_date.substring(0, 4) + "-" + dz_date.substring(4, 6) + "-" + dz_date.substring(6, 8);
				RefundInfoArgs args = new RefundInfoArgs();
				args.setStartDate(dz_date + " 00:00:00");
				args.setEndDate(dz_date + " 23:59:59");
				args.setMerchantNum(merchantnum);
				ExecuteCompWS ws= new ExecuteCompWS("powermop-dataacess_dz.properties");
				try {
					dzInfoArray =  (DZInfoArray) ws.getListBean(address, args, DZInfoArray.class);
				} catch (Exception e) {
					LOG.addError("获取民生医院数据异常"+e);
					dzInfoArray = new DZInfoArray();
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("获取his对账数据异常");
					return dzInfoArray;
				}
				if(null == dzInfoArray){
					dzInfoArray = new DZInfoArray();
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("获取对账数据为null");
					return dzInfoArray;
				}else{
					DZInfo[] array=dzInfoArray.getDzInfo();
					String record=null==array?"记录为null":"记录共"+array.length+"条";
					LOG.addInfo("调用MsHisServiceImpl成功，商户号："+merchantnum+",日期:"+dz_date+","+record);
					return dzInfoArray;
				}
			}else{
				dzInfoArray = new DZInfoArray();
				dzInfoArray.setRc("-1");
				if(null == msg) msg = "退费数据写入异常，请联系操作员";
				dzInfoArray.setErrormsg(msg);
				return dzInfoArray;
			}
		}
		return dzInfoArray;
	}

}
